import request from "@/utils/request";

const BASE_URL = "/api/admin";

const CategoryAPI = {
  /**
   * 查询单个分类
   */
  getAllCategory() {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, any>({
      url: `${BASE_URL}/getAllCategory`,
      method: "get",
    });
  },
  checkUpdCategoryName(category_id: number, category_name: string) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, Category>({
      url: `${BASE_URL}/checkUpdCategoryName`,
      params: { category_id, category_name },
      method: "get",
    });
  },
  /**
   * 查询单个分类
   */
  getCategory(category_id: number) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, Category>({
      url: `${BASE_URL}/getOneCategory`,
      params: { category_id },
      method: "get",
    });
  },
  /**
   * 添加分类
   */
  updCategory(category: Category) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, {}>({
      url: `${BASE_URL}/updCategory`,
      data: category, // // 服务器接收： req.body
      method: "put",
    });
  },
  /**
   * 添加分类
   */
  addCategory(category: Category) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, {}>({
      url: `${BASE_URL}/addCategory`,
      data: category, // // 服务器接收： req.body
      method: "post",
    });
  },
  /**
   * 删除多个分类
   */
  delManyCategory(category_ids: string) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, {}>({
      url: `${BASE_URL}/delManyCategory`,
      data: { category_ids }, // // 服务器接收： req.body
      method: "delete",
    });
  },
  /**
   * 删除单个分类
   */
  fetchDeleteCategory(category_id: number) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, {}>({
      url: `${BASE_URL}/category`,
      // params: { category_id }, // 服务器接收： req.query
      data: { category_id }, // // 服务器接收： req.body
      method: "delete",
    });
  },
  /**
   * 获取分页分类
   */
  fetchCategory({ page, pagesize, category_name, is_show, start_date, end_date }: QueryCate) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, CategoryPageData>({
      url: `${BASE_URL}/category`,
      params: { page, pagesize, category_name, is_show, start_date, end_date },
      method: "get",
    });
  },
};

export default CategoryAPI;

export interface QueryCate {
  page: number;
  pagesize: number;
  category_name?: string;
  is_show?: number | string;
  start_date?: string;
  end_date?: string;
}

export interface CategoryPageData {
  category: Category[];
  count: number;
}

export interface Category {
  category_id?: number;
  category_name: string;
  nickname?: string;
  sort: number;
  is_show: number | boolean;
  add_date: string;
  intro: string;
  admin_id: number;
}
